<?php
namespace app\admin\controller;
use think\console\Input;

use think\Request;

use think\Controller;
use think\Db;
class Admin extends Controller
{
public function admin()
   {
   	$list=Db::name("jf_admin")
   	        ->alias('a')
   	        ->join('jf_role r','a.g_id=r.r_id')
   	        ->paginate(2);
            
            $page = $list->render();

            $this->assign('list',$list);
            $this->assign('page', $page);
      return $this->fetch();

   }
   public function admin_add()
   {
   	  $request=Request::instance();
   	  
   	  if($request->has('sub','post'))
   	  {
   	    
   	     $a_name=$request->post('a_name');
   	     $a_pwd=$request->post('a_pwd');
   	     $g_id=$request->post('r_id');   	      
   	     $data=[
   	          
   	           'a_name'=>$a_name,
   	           'a_pwd'=>$a_pwd,
   	           'g_id'=>$g_id,          
   	     ];
   	   $result=Db::table('jf_admin')->insert($data);
   	   if($result)
   	   {
   	      $this->success('新增成功','Admin/admin');
   	   }
   	   else 
   	   {
   	      $this->error('新增失败');
   	   }
   	  }
   	  else 
   	  {   
   	  	 
   	     $a_id=input('?param.a_id');
   	     $jf_role=Db::table('jf_role')->select();
   	     $jf_admin=Db::table('jf_admin')->where('a_id',$a_id)->find();
   	     return $this->fetch('admin_add',[
   	                             'jf_admin'=>$jf_admin,
   	                             'jf_role'=>$jf_role,
   	     
   	         ]);
   	  }
      //return $this->fetch(); 
   }
   public function admin_update()
   {
    
   	$request=Request::instance();
  	if ($request->has("sub","post"))
  	{
	     $a_id=$request->param("a_id");
	     $a_name=$request->post('a_name');
   	     $a_pwd=$request->post('a_pwd');
   	     $g_id=$request->post('r_id');  
	  	 $data=[
	  	       'a_id'=>$a_id,   	           
   	           'a_name'=>$a_name,
   	           'a_pwd'=>$a_pwd,
   	           'g_id'=>$g_id,  	   	           
   	      ]; 	  	    
	     $result=Db::table("jf_admin")
		         ->update($data);
	  	if($result)
		  {$this->success("修改成功","Admin/admin");}
	    else 
		  {$this->error("修改失败");}
	  	}
  	else 
  	{
	  	$a_id=$request->param("a_id");
	  	$data=['a_id'=>$a_id]; 
	  	$jf_role=Db::table('jf_role')->select(); 
		$list=Db::name("jf_admin")
			  ->where('a_id',$a_id)
	          ->find();
	     return $this->fetch('admin_update',[
               'list'=>$list,
	           'jf_role'=>$jf_role,
       ]);        
  	}
   }
   public function admin_delete()
   {
      $request=Request::instance();
      $a_id=$request->param('a_id');
      $data=['a_id'=>$a_id];
      $result=Db::table('jf_admin')->delete($data);
       if($result)
      {
        $this->success('删除成功','Admin/admin');  
      }
      else 
      {
        $this->error('删除失败');
      }
      
   }
}
